package com.ygqh.baby.service.impl;
import java.util.Date;
import java.util.List;
import java.util.Map;

import  com.ygqh.baby.ao.QueryInfo;
import  com.ygqh.baby.ao.ResultSet;
import  com.ygqh.baby.po.YgOrderTrack;
import com.ygqh.baby.po.YgOrderTrackDetail;
import com.ygqh.baby.po.YgOrderTrackDetailExample;
import com.ygqh.baby.mapper.YgOrderTrackDetailMapper;
import  com.ygqh.baby.mapper.YgOrderTrackMapper;
import  com.ygqh.baby.service.YgOrderTrackService;

import  org.springframework.util.Assert;

import java.util.Arrays;

import  org.springframework.stereotype.Service;
import  org.springframework.beans.factory.annotation.Autowired;

import  com.ygqh.baby.po.YgOrderTrackExample;
import  com.ygqh.baby.ao.DataStatus;

@Service
public class YgOrderTrackServiceImpl implements YgOrderTrackService{
	
	@Autowired
	private YgOrderTrackMapper ygOrderTrackMapper;
	@Autowired
	private YgOrderTrackDetailMapper ygOrderTrackDetailMapper;
	
	@Override
	public List<YgOrderTrack> find() {
		YgOrderTrackExample example = new YgOrderTrackExample();		
		return ygOrderTrackMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgOrderTrackExample example = new YgOrderTrackExample();		
		return ygOrderTrackMapper.countByExample(example);
	}

	@Override
	public YgOrderTrack findById(Long id) {
		YgOrderTrack track= ygOrderTrackMapper.selectByPrimaryKey(id);
		YgOrderTrackDetailExample ex=new YgOrderTrackDetailExample();
		ex.createCriteria().andTrackIdEqualTo(id);
		track.setYgOrderTrackDetails(ygOrderTrackDetailMapper.selectByExample(ex));
		return track;
	}


	@Override
	public int save(YgOrderTrack ygOrderTrack) {
		ygOrderTrackMapper.insertSelective(ygOrderTrack);
		for(YgOrderTrackDetail d :ygOrderTrack.getYgOrderTrackDetails() ){
			d.setTrackId(ygOrderTrack.getId());
			ygOrderTrackDetailMapper.insert(d);
		}		
		return 1;
	}

	@Override
	public int update(YgOrderTrack ygOrderTrack) {
		return ygOrderTrackMapper.updateByPrimaryKeySelective(ygOrderTrack);
	}
	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(YgOrderTrack ygOrderTrack) {
		int r = 0;
		if(ygOrderTrack.getId() != null) {
			r = this.update(ygOrderTrack);
		}else {
			r = this.save(ygOrderTrack);
		}
		return r;
	}

	@Override
	public ResultSet<YgOrderTrack> search(QueryInfo queryInfo, String q,
			Date startDate, Date endDate, Boolean isFinished, String srcId,String workType) {
		List<YgOrderTrack>  list =  ygOrderTrackMapper.selectSuper(queryInfo, q,startDate,endDate,isFinished,srcId,workType);
		int count = ygOrderTrackMapper.countSuper(q,startDate,endDate,isFinished,srcId,workType);
		ResultSet<YgOrderTrack> resultSet = new ResultSet<YgOrderTrack>(count,list);
		return resultSet;
		
	}

	@Override
	public int deleteSoft(Long[] ids) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Map<String, Object>> searchList(String q,
			Date startDate, Date endDate, Boolean isFinished, String srcId,
			String workType) {
		return ygOrderTrackMapper.selectList(q, startDate, endDate, isFinished, srcId, workType);
		
	}

}

